<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/**
 * @param $url get请求地址
 * @param int $httpCode 返回状态码
 */


use think\Log;



function curl_get($url,$httpCode=0) {
    $ch=curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
    $file_contents=curl_exec($ch);
    $httpCode=curl_getinfo($ch,CURLINFO_HTTP_CODE);
    curl_close($ch);
    return $file_contents;
}

function getRandChar($length){
    $str = null;
    $strPol = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $max = strlen($strPol) - 1;

    for($i = 0;$i < $length; $i++){
        $str .= $strPol[rand(0,$max)];
    }

    return $str;
}


//公共返回信息
function errorRespone($msg, $code = 1, $data = [])
{
    $data = empty($data) ? new \stdClass() : $data;
    ajaxReturn($code, $msg, $data);
}

function successRespone($msg, $data = [])
{
    $data = empty($data) ? new \stdClass() : $data;
    ajaxReturn(0, $msg, $data);
}


/**
 * 返回消息
 * @param  int    $code        状态码
 * @param  string $msg         提示消息
 * @param  array  $data        数据
 * @return string
 */
function ajaxReturn($code, $msg, $data, $json_option = JSON_UNESCAPED_UNICODE)
{
    $data = $data == null? array():$data;
    $return = [
        "code" => $code,
        "msg" => $msg,
        "data" => $data,
    ];
    $jsonData = json_encode($return, $json_option);

    header('Content-type: application/json'); //设置json头标志
    exit(str_replace('null', '""', $jsonData));
}




/**
 * 发送HTTP请求方法
 * @param string $url  请求URL
 * @param array $params 请求参数
 * @param string $method 请求方法GET/POST
 * @return array $data  响应数据
 */
function http_send($url, $params, $method = 'GET', $header = array(), $multi = false){
    $opts = array(
        CURLOPT_TIMEOUT    => 30,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_HTTPHEADER   => $header
    );
    /* 根据请求类型设置特定参数 */
    switch(strtoupper($method)){
        case 'GET':
            $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
            break;
        case 'POST':
            //判断是否传输文件
            $params = $multi ? $params : http_build_query($params);
            $opts[CURLOPT_URL] = $url;
            $opts[CURLOPT_POST] = 1;
            $opts[CURLOPT_POSTFIELDS] = $params;
            break;
        default:
            throw new Exception('不支持的请求方式！');
    }
    /* 初始化并执行curl请求 */
    $ch = curl_init();
    curl_setopt_array($ch, $opts);
    $data = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);
    if($error) throw new Exception('请求发生错误：' . $error);
    return $data;
}